package com.sm.chatinputlibrary.emoji;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import android.util.Log;

import java.io.IOException;
import java.util.Objects;

/**
 * created by zhud on 2019/4/2
 */
public class EmojiBean implements Parcelable {
    /**
     * 图片对应的unicode码
     */
    @NonNull
    private final String unicode;
    /**
     * 资源文件id
     */
    @NonNull
    private final String resourcePath;
    /**
     * 别名格式为[中文]
     */
    @NonNull
    private final String emojiAlias;

    public EmojiBean(@NonNull String unicode, @NonNull String resourcePath, @NonNull String emojiAlias) {
        this.unicode = unicode;
        this.resourcePath = resourcePath;
        this.emojiAlias = emojiAlias;
    }

    private EmojiBean(Parcel in) {
        unicode = Objects.requireNonNull(in.readString());
        resourcePath = Objects.requireNonNull(in.readString());
        emojiAlias = Objects.requireNonNull(in.readString());
    }

    public static final Creator<EmojiBean> CREATOR = new Creator<EmojiBean>() {
        @Override
        public EmojiBean createFromParcel(Parcel in) {
            return new EmojiBean(in);
        }

        @Override
        public EmojiBean[] newArray(int size) {
            return new EmojiBean[size];
        }
    };

    @NonNull
    public String getUnicode() {
        return unicode;
    }

    @NonNull
    public String getResourcePath() {
        return resourcePath;
    }

    @NonNull
    public String getEmojiAlias() {
        return emojiAlias;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @NonNull
    public Drawable getDrawable(final Context context) {
        try {
            return Drawable.createFromStream(context.getAssets().open(resourcePath), null);
        } catch (IOException e) {
            Log.d(this.getClass().getSimpleName(), "找不到表情:" + resourcePath);
            return null;
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(unicode);
        dest.writeString(resourcePath);
        dest.writeString(emojiAlias);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final EmojiBean emoji = (EmojiBean) o;

        return resourcePath.equals(emoji.resourcePath)
                && unicode.equals(emoji.unicode);
    }

    @Override
    public int hashCode() {
        int result = unicode.hashCode();
        result = 31 * result + resourcePath.hashCode();
        return result;
    }
}
